//
//  SearchViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "KindModel.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *arr;
@property(nonatomic, strong)NSMutableDictionary *dic;
@property (nonatomic,retain)UILabel *label;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [NSMutableArray array];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 - 36, self.view.bounds.size.width,self.view.bounds.size.height) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"search"];
    
    self.label =[[UILabel alloc]initWithFrame:self.view.bounds];
    self.label.text =@"亲,没有相关动画╭(╯3╰)╮";
    self.label.font =[UIFont systemFontOfSize:20];
    self.label.hidden =YES;
    self.label.textColor =[UIColor blackColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:self.label];
    
    [self json];
}

-(void)json
{
    NSString *str =[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Search/comicSearchListForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/943AAD67-2C90-414B-A21F-D9BE0A1E80F6/key_word/%@/page/1/listcnt/20",self.text];
    [NetHandler dataWithUrl:str complant:^(NSData *data) {
        self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [self.dic objectForKey:@"data"];
        for (NSDictionary *dict in arr) {
            KindModel *kind = [[KindModel alloc]init];
            [kind setValuesForKeysWithDictionary:dict];
            [self.arr addObject:kind];
        }
        if (self.arr.count ==0) {
            self.label.hidden =NO;
        }
        [self.tableView reloadData];
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KindModel *kind = [self.arr objectAtIndex:indexPath.row];
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search" forIndexPath:indexPath];
    cell.label.text = kind.title ;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:kind.cover_url]];

//    // 关键字高亮
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:[kind.title stringByRemovingPercentEncoding]];
    NSRange range = [[kind.title stringByRemovingPercentEncoding] rangeOfString:[self.text stringByRemovingPercentEncoding]];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    cell.label.attributedText = string;
        return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
